﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MVCManage.Model.Base;
using MVCManage.Model;
using MVCManage.DAL;
using System.Data.Objects;

namespace MVCManage.BLL
{
    public class PderBLL : BaseBLL<pder>
    {
        public override void SetDal()
        {
            Dal = new PderDAL();
        }

        public string hhh()
        {
            return new PderDAL().hhh();
        }
        /// <summary>
        /// 订单显示
        /// </summary>
     
        ///   <param name="dasj">大于时间</param>
        ///    <param name="minsj">小于时间</param>
        /// <param name="page">页码</param>
        /// <param name="size">页数</param>
        /// <param name="count">总条目</param>
        /// <param name="ddzt">下单状态 如 制作中</param>
        /// <param name="xdzt"> 在店吃 or  在外面吃</param>
        /// <returns></returns>
        public List<pder> GetPdersList(int page,int size,out int count ,int ddzt, int xdzt,DateTime ATime ,DateTime Btime)
        {
             
            var zhi = Dal.Query();
            if (ddzt <=5) 
            {
                zhi = zhi.Where(n => n.zt == ddzt);
            }

            if (xdzt < 3)
            {
                zhi = zhi.Where(n => n.ptype == xdzt);
            }
            if (ATime!=null&&Btime!=null) 
            {
                zhi = zhi.Where(n => n.pdata>ATime&&n.pdata<Btime);
            }

            if (ATime == null && Btime!= null)
            {
                zhi = zhi.Where(n =>n.pdata < Btime);
            }

            if (ATime != null && Btime == null)
            {
                zhi = zhi.Where(n => n.pdata > ATime);
            }
            count = zhi.Count();
            var pagelist = zhi.OrderBy(n => n.pbh).Skip((page - 1)*size).Take(size).ToList();

            return pagelist;
        }

        public List<pder>chay(int page, int size, out int count, string name,DateTime sj1,DateTime sj2)
        {
            var zhi = Dal.Query().Where(n=>n.zt>3&&n.pdata>=sj1&&n.pdata<=sj2);
           
            if (!string.IsNullOrWhiteSpace(name))
            {
                 zhi = zhi.Where(n => n.Users.userName.Contains(name));
            }
        
            count = zhi.Count();
            var pagelist = zhi.OrderBy(n => n.pbh).Skip((page - 1) * size).Take(size).ToList();

            return pagelist;
        }

        public bool Updatepder(pder dd)
        {
            var zz = Dal.Query().Where(n => n.pbh == dd.pbh).FirstOrDefault();
            zz.dbh = dd.dbh;
            zz.zhifu = dd.zhifu;
            zz.dizhi = dd.dizhi;
            zz.pdata = dd.pdata;
            zz.zt = dd.zt;
            zz.userID = dd.userID;
            var zz1 = new PderxqDAL();
          
            zz1.Deletes(zz.pderxq.ToList());
            zz1.Add(dd.pderxq.ToList());
            Dal.Update(zz);
            return Dal.SaveChanges();
        }
        public bool Deletepder(string dd)
        {
            var zz = Dal.Query().Where(n => n.pbh == dd).FirstOrDefault();         
            var zz1 = new PderxqDAL();
            zz1.Deletes(zz.pderxq.ToList());
            Dal.Delete(zz);          
            return Dal.SaveChanges();
        }
    }
}
